<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Main_url extends CI_Controller {

	public $sidebar_content='';
	public $sidebar_select='';

	function __construct()
	{
		parent::__construct();

		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		$this->load->library('ion_auth');
		$this->load->database();
		//$this->load->library('session');

	}

	function index($id='')
	{
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login','refresh');
		}

		$status = 'add';
		$data['name'] = '';
		$data['url'] = 'http://';
		$data['type'] = '';
		$data['order'] = '';
		$data['id'] = '';

		$id = intval($id);
		if($id == '')
		{
			$id = $this->input->post('id');
			$data['id'] = $id;

		}
		if($id != '')
		{
			$query = $this->db->from('main_url')->where('id',$id)->limit(1)->get();
			if($query->row())
			{
				$status = 'edit';
				foreach($query->result() as $row)
				{
					$data['id'] = $row->id;
					$data['name'] = $row->name;
					$data['url'] = $row->url;
					$data['type'] = $row->type;
					$data['order'] = $row->order;
				}
			}
			$data['legend'] = '编辑分类导航URL';

		} else {

			$data['legend'] = '添加分类导航URL';
		}

		$query = $this->db->get('main');
        $sidebar_names = $query->result();

		foreach($sidebar_names as $sidebar_name)
		{
			$this->sidebar_content .=  '<div class="box">
			<div class="quiet">'.$sidebar_name->name."<ul>";

			$sidebar_url_query = $this->db->from('main_url')->where('type',$sidebar_name->id)->order_by('order','desc')->get();

			foreach($sidebar_url_query->result() as $sidebar_url_array)
			{
				$href = site_url("main_url/index/".$sidebar_url_array->id."");
				$href_delete = site_url("main_url/delete_by_id/".$sidebar_url_array->id."");
				$this->sidebar_content .= <<<sidebar_url
				<li><a href="{$sidebar_url_array->url}" target="_blank">
				$sidebar_url_array->name
				</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="{$href}">编辑</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="{$href_delete}" onclick="return confirm('确定要删除吗？')">删除</a></li>
sidebar_url;
			}
			$this->sidebar_content .= '</ul></div></div>';

			if($sidebar_name->id == $this->input->post('type'))
				$this->sidebar_select .=  '<option value="'.$sidebar_name->id.'" selected="selected">'.$sidebar_name->name.'</option>';
			else if($sidebar_name->id == $data['type'])
				$this->sidebar_select .=  '<option value="'.$sidebar_name->id.'" selected="selected">'.$sidebar_name->name.'</option>';
			else
				$this->sidebar_select .=  '<option value="'.$sidebar_name->id.'">'.$sidebar_name->name.'</option>';
		}

		$this->load->model('nav_model');
		$this->load->model('ion_auth_model');

		$data['title'] = '分类导航URL';
		$data['include'] = 'main_url_view';// include file here
		$data['sidebar_title'] = '边栏名';
		$data['sidebar_content'] = $this->sidebar_content;
		$data['nav_view'] = $this->nav_model->get_nav('分类导航URL');
		$data['login_info'] = $this->ion_auth_model->get_user_info();


		$sidebar = <<<sidebar
			<select name="type">
			<option value="" selected="selected"></option>
		    {$this->sidebar_select}
			</select>
sidebar;
		$data['sidebar'] = $sidebar;

		$defaule_order = '<select name="order"';
		for($i = 0;$i <= 100;$i++)
		{
			if($i == $this->input->post('order'))
				$defaule_order .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
			else if($i == $data['order'])
				$defaule_order .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
			else if($i == 10)
				$defaule_order .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
			else
				$defaule_order .= '<option value="'.$i.'">'.$i.'</option>';
		}
		$defaule_order .= '</select>';

		$data['order'] = $defaule_order;

		$config = array(
               array(
                     'field'   => 'name',
                     'label'   => '网站名',
                     'rules'   => 'trim|required|xss_clean|callback_contentname_check|callback_unique_check'
					),
			array(
                     'field'   => 'url',
                     'label'   => 'URL',
                     'rules'   => 'trim|required|xss_clean|callback_contentname_check|callback_unique_check|prep_url'
					),

		array(
                     'field'   => 'type',
                     'label'   => '内容分类',
                     'rules'   => 'trim|required'
					),

            );
		if($status == 'edit')
		{
			$config = array(
               array(
                     'field'   => 'name',
                     'label'   => '网站名',
                     'rules'   => 'trim|required|xss_clean|callback_contentname_check'
					),
			array(
                     'field'   => 'url',
                     'label'   => 'URL',
                     'rules'   => 'trim|required|xss_clean|callback_contentname_check'
					),

			array(
                     'field'   => 'type',
                     'label'   => '内容分类',
                     'rules'   => 'trim|required'
					),

            );
		}
		$this->form_validation->set_rules($config);
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');

		//$this->form_validation->set_rules('password', 'Password', 'required');
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('global_view',$data);
		}
		else
		{
			if($status == 'edit') {
				$this->db->where('id', $id);
				$this->db->update('main_url', $_POST);
			}else
			{
				$this->db->insert('main_url', $_POST);
			}
			// clear cache
			$this->load->model('clear_cache_model');
			$this->clear_cache_model->clear_cache();

			redirect('main_url/index','refresh');

		}
	}

	function delete_by_id($id='')
	{
		if($id=='')
			return;
		if (!$this->ion_auth->logged_in())
		{
			redirect('auth/login','refresh');
		}

		$id = intval($id);

		$query = $this->db->from('main_url')->where('id',$id)->limit(1)->get();
		if($query->row())
		{
			$this->db->where('id', $id);
			$this->db->delete('main_url');

		}
		redirect('main_url/index','refresh');

	}

	function contentname_check($str)
	{
		$check_array = array('test','1234');
		if (in_array(strtolower($str),$check_array))
		{
			$this->form_validation->set_message('contentname_check', '%s不能为'.$str);
			return FALSE;
		}
		else
		{
			return TRUE;
		}
	}

	function unique_check($str)
	{
		$query = $this->db->select('name')->from('main_url')->where('name',$str)->limit(1)->get();

		if ($query->row()) {
 			$this->form_validation->set_message('unique_check', '%s已存在'.$str);
           return false;
        } else {
            return true;
        }
	}


}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */